<script setup>
import { onMounted, ref, watch } from "vue";
import * as echars from "echarts";

let props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});
const target = ref(null);
//1初始化echart实例
let mChart = null;

onMounted(() => {
  mChart = echars.init(target.value);
  renderChart();
});
//2.构建option配置对象
const renderChart = () => {
  const options = {
    xAxis: {
      type: "category",
      data: props.data.servers.map((item) => item.name),
      axisLable: {
        color: "#9eb1c8",
      },
    },
    yAxis: {
      type: "value",
      show: false,
      max: function (value) {
        return parseInt(value.max * 1.2);
      },
    },
    grid: {
      top: 16,
      right: 0,
      bottom: 26,
      left: 0,
      containLabel: true,
    },
    series: [
      {
        type: "bar",
        data: props.data.servers.map((item) => ({
          name: item.name,
          value: item.value,
        })),
        showBackground: true,
        // 每个轴的样式
        itemStyle: {
          color: "#479AD3", // 设置柱子的颜色
          // barBorderRadius: 5, // 设置柱子的圆角
          borderRadius: 5,
          shadowColor: "rgba(0, 0, 0, 0.3)", // 设置柱子的阴影颜色
          shadowBlur: 5, // 设置柱子的阴影模糊大小
        },
        // 轴宽度
        barWidth: 12,
        // 轴上的字体
        label: {
          show: true,
          // 设置标签位置为右侧
          position: "top",
          textStyle: {
            // 设置标签文本颜色
            color: "#fff",
          },
          formatter: '{c}%'
        },
      },
    ],
  };
  mChart.setOption(options);
};
watch(()=>props.data,renderChart)
</script>

<template>
  <h1>竖向柱状图</h1>
  <div>[服务资源占用比]</div>
  <div ref="target" class="w-full h-full"></div>
</template>

<style scoped>
</style>
